package com.zzb.myshowme.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzb.myshowme.MyApplication;
import com.zzb.myshowme.R;
import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.utils.UserInfoUtils;
import com.zzb.myshowme.utils.CustomToast;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    private boolean isWantToExitApp = false;

    private View live_layout;
    private View channel_layout;
    private View showme_layout;
    private View chat_layout;
    private View follow_layout;

    private ImageView bar_imageV;
    private ImageView live_imageV;
    private ImageView channel_imageV;
    private ImageView showme_imageV;
    private ImageView chat_imageV;
    private ImageView follow_imageV;


    FrameLayout fragmentcontainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        fragmentcontainer = (FrameLayout) findViewById(R.id.fragmentcontainer);

        live_layout = findViewById(R.id.live_layout);
        channel_layout = findViewById(R.id.channel_layout);
        showme_layout = findViewById(R.id.showme_layout);
        chat_layout = findViewById(R.id.chat_layout);
        follow_layout = findViewById(R.id.follow_layout);

        bar_imageV = (ImageView) findViewById(R.id.bar_imageV);
        live_imageV = (ImageView) findViewById(R.id.live_imageV);
        channel_imageV = (ImageView) findViewById(R.id.channel_imageV);
        showme_imageV = (ImageView) findViewById(R.id.showme_imageV);
        chat_imageV = (ImageView) findViewById(R.id.chat_imageV);
        follow_imageV = (ImageView) findViewById(R.id.follow_imageV);


        bar_imageV.setOnClickListener(this);
        live_layout.setOnClickListener(this);
        channel_layout.setOnClickListener(this);
        showme_layout.setOnClickListener(this);
        showme_imageV.setOnClickListener(this);
        chat_layout.setOnClickListener(this);
        follow_layout.setOnClickListener(this);

        setAllDefautSelect();
    }
//    @Override
//    public void onBackPressed() {
//        if (isWantToExitApp) {
////            stopNoticeService();
//            super.onBackPressed();
//            return;
//        }
//        isWantToExitApp = true;
//        CustomToast.show(this, getString(R.string.exit_app_tips), Toast.LENGTH_LONG);
//        getWindow().getDecorView().postDelayed(mClearExitAppFlagRunnable, 3000);
//    }
//
//    private Runnable mClearExitAppFlagRunnable = new Runnable() {
//        @Override
//        public void run() {
//            isWantToExitApp = false;
//        }
//    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_imageV:
                Intent intent = new Intent(MainActivity.this, UserCenterActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.move_in_top, R.anim.move_out_bottom);
                break;
            case R.id.live_layout:
                setAllDefautSelect();
                live_imageV.setSelected(true);
                break;
            case R.id.channel_layout:
                setAllDefautSelect();
                channel_imageV.setSelected(true);
                break;
            case R.id.showme_imageV:
            case R.id.showme_layout:
                setAllDefautSelect();
                showme_imageV.setSelected(true);
                UserInfo user = UserInfoUtils.readLoginMember(MyApplication.getInstance());

                break;
            case R.id.chat_layout:
                setAllDefautSelect();
                chat_imageV.setSelected(true);
                break;
            case R.id.follow_layout:
                setAllDefautSelect();
                follow_imageV.setSelected(true);
                break;

        }
    }

    protected void setAllDefautSelect() {
        live_imageV.setSelected(false);
        channel_imageV.setSelected(false);
        showme_imageV.setSelected(false);
        chat_imageV.setSelected(false);
        follow_imageV.setSelected(false);
    }

    private long exitTime = 0;
    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            CustomToast.show(this, getString(R.string.exit_app_tips), Toast.LENGTH_LONG);
            exitTime = System.currentTimeMillis();
        } else {
//            finish();
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
        }
    }
}
